<?php
//
//	file: includes/ucp/ucp_advmsg_pm_read.php
//	begin: 10/07/2006
//	$Author: christopher $
//	$Revision: 28 $
//	$Date: 2008-01-28 23:55:41 +0000 (Mon, 28 Jan 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: using post/get data, this processes and displays an existing pm.

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

// simple class to display a pm
class advnot_readpm extends form
{
	var $requester;
	var $parms;
	var $handler;

	var $data;
	var $box_type;
	var $box_id;
	var $box;

	var $boxes;
	var $boxes_map;

	function advnot_readpm($requester, $parms='', &$fields)
	{
		global $config, $view_user, $dft_folder;

		$this->requester = $requester;
		$this->now = time();
		$this->parms = empty($parms) ? array() : $parms;
		$this->data = $this->boxes = $this->privmsg_ids = $this->boxes_map = $this->delete_box = $this->privmsgs_ids = array();
		$this->total = $this->ppage = $this->start = 0;

		// load box info
		$pm_boxes = new advmsg_pm_boxes();
		$this->boxes = $pm_boxes->get_boxes($view_user->data['user_id']);
		$this->boxes_map = $pm_boxes->get_boxes_map($view_user->data['user_id']);
		unset($pm_boxes);
		$this->box = _read('folder', TYPE_NO_HTML, $dft_folder, $this->boxes_map);
		$this->box_id = $this->boxes_map[$this->box];
		$this->box_type = $this->boxes[$this->box_id]['pm_box_type'];
		$this->msg_id = _read('p', TYPE_INT, 0);

		// load attachment for quota
		if ( $config->data['mod_attachment_CH'] )
		{
			include($config->url('includes/class_thumbnail'));
			include($config->url('includes/class_attach_parent'));
			include($config->url('includes/class_attach_privmsg'));

			$this->attachment = new attach_privmsg();
			$this->attachment->action();
			$this->attachment->init($this->box);
		}

		parent::form($fields);
	}

	function process()
	{
		if ( $this->read() )
		{
			$this->check();
			$this->validate();
			$this->display();
			return true;
		}
		return false;
	}

	// read form data
	function read()
	{
		global $pm;
		return $pm->read();
	}

	// check data for errors
	function check()
	{
		global $pm;
		return $pm->check();
	}

	// process data for post/preview
	function validate()
	{
		global $pm;
		return $pm->validate();
	}

	// display form
	function display()
	{

		global $template, $user, $config, $pm;
		global $view_user, $warning, $warning_msg;
		global $sort_fields, $order_fields;
		global $dft_sort, $dft_order, $dft_save_folder;

		// give warning
		if ( $warning )
		{
			$template->assign_block_vars('warning', array(
				'WARNING_TITLE' => $user->lang('Information'),
				'WARNING_MSG' => $warning_msg,
			));
		}
		// send delete confirm
		if ( _button('delete_details') && !empty($this->select_box) && !_button('cancel_details') && !_button('confirm_details') )
		{
			$template->assign_vars(array(
				'L_FORM' => $user->lang('Confirm'),
				'MESSAGE' => $user->lang('Confirm_delete_pms'),
			));
			display_buttons(array(
				'confirm_details' => array('txt' => 'Delete', 'img' => 'cmd_delete', 'key' => 'cmd_delete'),
				'cancel_details' => array('txt' => 'Cancel', 'img' => 'cmd_cancel', 'key' => 'cmd_cancel'),
			));
			$template->set_switch('form');
			$template->assign_vars(array('FORM' => $template->include_file('ucp/ucp_confirm_box.tpl')));

			// hide delete
			_hide('delete_details', true);

			// hide selection values
			if ( !empty($this->data) )
			{
				foreach ( $this->data as $idx => $data )
				{
					if ( isset($this->select_box[ ($idx + 1) ]) )
					{
						_hide('select_box[]', $idx + 1, true);
					}
					_hide('privmsgs_ids[]', $this->data[$idx]['privmsgs_id'], true);
				}
			}

			// hide search values
			foreach ( $this->fields as $field_name => $field )
			{
				_hide($field_name, $this->fields[$field_name]->form_escape_string($this->fields[$field_name]->value));
			}
			return;
		}

		// display details
		if ( $count_data = count($this->data) )
		{
			$row_to_color = $row_from_color = false;
			for ( $i = 0; $i < $count_data; $i++ )
			{
				$row = $this->data[$i];
				$privmsgs_id = intval($row['privmsgs_id']);
				$flag = $row['privmsgs_type'];
				$icon_flag = ( $flag == PRIVMSGS_NEW_MAIL || $flag == PRIVMSGS_UNREAD_MAIL ) ? $user->img('pm_unreadmsg') : $user->img('pm_readmsg');
				$icon_flag_alt = ( $flag == PRIVMSGS_NEW_MAIL || $flag == PRIVMSGS_UNREAD_MAIL ) ? $user->lang('Unread_message') : $user->lang('Read_message');

				// Split the messages (for saveboxes) into to/from current user
				$row_type = (intval($row['privmsgs_from_userid']) == $view_user->data['user_id']) ? 'row_from' : 'row_to';
				$row_color = $row_type . '_color';
				$$row_color = !$$row_color;

				$template->assign_block_vars('row', array(
					'ROW_NUMBER' => $i + 1,
					'PRIVMSGS_ID' => intval($row['privmsgs_id']),
					'U_MESSAGE' => $config->url('usercp', array('mode'=>'privmsg', 'sub'=>'pm_read', POST_POST_URL => $row['privmsgs_id']), true),

					'FLAG' => $user->lang('Flag'),
					'SUBJECT' => $user->lang('Subject'),
					'DATE' => $user->lang('Date'),
					'FROM' => $user->lang('From'),
					'MARK_ALL' => $user->lang('Mark_all'),
				));
				$template->set_switch($row_type . '.light', $$row_color);
			}
		}

		// display header
		if ($user->auth(POST_GROUP_URL, 'ucp_pm_new', GROUP_REGISTERED))
		{
			$post_pm = $config->url('usercp', array('mode'=>'privmsg', 'sub'=>'pm_post', 'mode'=>'post'), true);
			$post_pm_img = '<a href="' . $post_pm . '"><img src="' . $user->img('pm_postmsg') . '" alt="' . $user->lang('Post_new_pm') . '" border="0" /></a>';
			$post_new_mesg_url = '<a href="' . $post_pm . '"><img src="' . $user->img('post_new') . '" alt="' . $user->lang('Send_a_new_message') . '" border="0" /></a>';
			$post_pm = '<a href="' . $post_pm . '">' . $user->lang('Post_new_pm') . '</a>';
		}
		else
		{
			$post_pm = '';
			$post_pm_img = '';
		}

		$template->assign_vars(array(
			'BOX_NAME' => $this->boxes[$this->box_id]['pm_box_txt'],
			'BOX_IMG' => $this->boxes[$this->box_id]['pm_box_img'],

			'POST_PM_IMG' => $post_pm_img,
			'POST_PM' => $post_pm,

			'L_MARK' => $user->lang('Mark'),
			'L_FLAG' => $user->lang('Flag'),
			'L_SUBJECT' => $user->lang('Subject'),
			'L_DATE' => $user->lang('Date'),
			'L_DISPLAY_MESSAGES' => $user->lang('Display_messages'),
			'L_FROM' => $user->lang('From'),
			'L_TO' => $user->lang('To'),
			'L_MARK_ALL' => $user->lang('Mark_all'),
			'L_UNMARK_ALL' => $user->lang('Unmark_all'),
			'L_DELETE_MARKED' => $user->lang('Delete_marked'),
			'L_DELETE_ALL' => $user->lang('Delete_all'),
			'L_SAVE_MARKED' => $user->lang('Save_marked'),

			'S_PRIVMSGS_ACTION' => $config->url('usercp', array('mode'=>'privmsg', 'sub'=>'pm_box', 'folder'=>$this->box), true),
		));

		$template->set_switch('details');
		$details_buttons = array(
			'delete_details' => array('txt' => 'Delete_marked', 'img' => 'cmd_delete', 'key' => 'cmd_delete'),
			'save_details' => array('txt' => 'Save_marked', 'img' => 'cmd_save', 'key' => 'cmd_save'),
		);
		display_buttons($details_buttons, 'details');

		$parms = $this->get_link_parms();
		$pagination = new pagination($this->requester, $parms);
		$pagination->display('pagination', $this->total, $this->ppage, $this->start, true, 'Attachments_count');
		unset($pagination);

		// send all to template
		$template->assign_block_vars('cp_content', array('BOX' => $template->include_file('ucp/ucp_advmsg_pm_box.tpl')));

		// attachment quota box
		if ( $config->data['mod_attachment_CH'] )
		{
			$this->display_attach_quota();
		}
	}

	// @private display pm attachments quota box
	function display_attach_quota()
	{
		global $template, $db, $user, $config;
		global $attach_config, $attachment;
		global $view_user;
		global $cp_no_menus, $cp_menu_title, $cp_requester, $cp_parms, $cp_panels, $menus, $sub_menus, $ctx_menus, $menu_id, $subm_id, $ctx_id;

		// we handle the menu to display it first
		$cp_no_menus = true;
		$template->assign_vars(array(
			'L_MENU' => $cp_menu_title,
		));
		$cp_panels->display_menus($menus, $sub_menus, $ctx_menus, $menu_id, $subm_id, $ctx_id, $cp_requester, $cp_parms);

		// get quota size
		$attachment->get_quota_limits($user->data, PAGE_PRIVMSGS);
		$filesize_limit_value = intval($attach_config->data['upload_filesize_limit']) ? intval($attach_config->data['upload_filesize_limit']) : intval($attach_config->data['attachment_quota']);
		if ( empty($filesize_limit_value) )
		{
			$filesize_limit_unit = '';
			$filesize_limit = $user->lang('Unlimited');
		}
		else
		{
			$units = array();
			$filesize_limit_unit = size_get_units($filesize_limit_value, $units);
			$filesize_limit = round($filesize_limit_value / max(1, $filesize_limit_unit), 2) . ' ' . $user->lang($units[$filesize_limit_unit]);
		}

		// get upload size (only posts)
		$from_user = array(PRIVMSGS_SENT_MAIL, PRIVMSGS_NEW_MAIL, PRIVMSGS_SAVED_OUT_MAIL);
		$to_user = array(PRIVMSGS_READ_MAIL, PRIVMSGS_NEW_MAIL, PRIVMSGS_UNREAD_MAIL, PRIVMSGS_SAVED_IN_MAIL);
		$sql = 'SELECT SUM(d.filesize) AS sum_filesize
					FROM ' . ATT_DESC_TABLE . ' d, ' . ATT_LINKS_TABLE . ' a, ' . PRIVMSGS_TABLE . ' p
					WHERE d.attach_id = a.attach_id
						AND p.privmsgs_id = a.privmsgs_id
						AND a.post_id = 0
						AND ((p.privmsgs_from_userid = ' . intval($view_user->data['user_id']) . ' AND p.privmsgs_type IN(' . implode(', ', $from_user) . '))
							OR (p.privmsgs_to_userid = ' . intval($view_user->data['user_id']) . ' AND p.privmsgs_type IN(' . implode(', ', $to_user) . ')))';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$filesize_upload_value = ($row = $db->sql_fetchrow($result)) ? intval($row['sum_filesize']) : 0;
		$db->sql_freeresult($result);

		$units = array();
		$filesize_upload_unit = size_get_units($filesize_upload_value, $units);
		$filesize_upload = round($filesize_upload_value / max(1, $filesize_upload_unit), 2) . ' ' . $user->lang($units[$filesize_upload_unit]);

		// then we display the quota block
		$template->assign_block_vars('attach_quota', array(
			'L_QUOTA' => sprintf($user->lang('User_quota_profile'), $filesize_limit),
			'L_UPLOADED' => sprintf($user->lang('User_uploaded_profile'), $filesize_upload),
			'QUOTA_BOX' => get_quota_box('Upload_percent_profile', $filesize_upload_value, $filesize_limit_value),
		));
		$template->assign_block_vars('cp_menus', array('BOX' => $template->include_file('ucp/ucp_attach_quota_box.tpl', array('attach_quota'))));
	}
}

// process
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);
$form = new advnot_readpm($cp_requester, $cp_parms + $parms, $fields);
$form->process();
$template->set_filenames(array('body' => 'cp_generic.tpl'));

?>